package top.drinyeit.weilong.fragment;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.acker.simplezxing.activity.CaptureActivity;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.activity.BalanceActivity;
import top.drinyeit.weilong.activity.EnterpriseCertificationActivity;
import top.drinyeit.weilong.activity.LoginActivity;
import top.drinyeit.weilong.activity.MessageActivity;
import top.drinyeit.weilong.activity.MyAddressActivity;
import top.drinyeit.weilong.activity.MyOrderActivity;
import top.drinyeit.weilong.activity.PersonalDataActivity;
import top.drinyeit.weilong.activity.SetupActivity;
import top.drinyeit.weilong.app.Constant;
import top.drinyeit.weilong.base.LazyFragment;
import top.drinyeit.weilong.presenter.MinePresenter;
import top.drinyeit.weilong.util.SPUtil;
import top.drinyeit.weilong.view.CircleImageView;

/**
 * Created by Administrator on 2017/11/10.
 * last change on 18/3/21
 */

public class MineFragment extends LazyFragment implements View.OnClickListener {


    private static final int REQ_CODE_PERMISSION = 0x1111;

    private ImageView userHead; //用户头像
    private TextView loginRegistration; //登录注册
    private RelativeLayout head1;//
    public CircleImageView head2;//登录后头像
    public TextView userName; //用户名
    private ImageView lv;//用户等级图标
    public ImageView level;//用户等级
    private ImageView scan; //扫一扫

    private LinearLayout waitingList;//待接单
    private LinearLayout waitingSend;//待配送
    private LinearLayout waitingSign;//待签收
    private LinearLayout overOrder;//已完成
    private LinearLayout allOrder;//全部订单

    private RelativeLayout myAddress;//收货地址
    private RelativeLayout mineMessage;//我的消息
    private RelativeLayout myEvaluation;//我的评价
    private RelativeLayout myMoney;//我的余额
    private RelativeLayout qiyerenzheng;//企业认证
    private RelativeLayout setting;//设置

    private String s ;

    private MinePresenter minePresenter;

    static boolean back = true;


    @Override
    protected int attachLayoutRes() {
        return R.layout.frag_mine;
    }

    @Override
    protected void initViews() {
        userHead = mRootView.findViewById(R.id.userHead);
        loginRegistration = mRootView.findViewById(R.id.loginRegistration);
        head1 = mRootView.findViewById(R.id.head1);
        head2 = mRootView.findViewById(R.id.head2);
        userName = mRootView.findViewById(R.id.userName);
        lv = mRootView.findViewById(R.id.lv);
        level = mRootView.findViewById(R.id.level);
        scan = mRootView.findViewById(R.id.scan);

        waitingList = mRootView.findViewById(R.id.waitingList);
        waitingSend = mRootView.findViewById(R.id.waitingSend);
        waitingSign = mRootView.findViewById(R.id.waitingSign);
        overOrder = mRootView.findViewById(R.id.overOrder);
        allOrder = mRootView.findViewById(R.id.allOrder);

        myAddress = mRootView.findViewById(R.id.myAddress);
        mineMessage = mRootView.findViewById(R.id.mineMessage);
        myEvaluation = mRootView.findViewById(R.id.myEvaluation);
        myMoney = mRootView.findViewById(R.id.myMoney);
        qiyerenzheng = mRootView.findViewById(R.id.qiyerenzheng);
        setting = mRootView.findViewById(R.id.setting);

        inmV();

    }


    @Override
    protected void lazyLoad() {
        minePresenter = new MinePresenter(this);
        isLogin();
    }

    private void isLogin() {
        s = SPUtil.getShareString(Constant.USER_ID);
        if ("".equals(s)) {
            userHead.setVisibility(View.VISIBLE);
            loginRegistration.setVisibility(View.VISIBLE);
            head1.setVisibility(View.GONE);
            userName.setVisibility(View.GONE);
            lv.setVisibility(View.GONE);
            level.setVisibility(View.GONE);
        } else {
            loginRegistration.setVisibility(View.GONE);
            userHead.setVisibility(View.GONE);
            head1.setVisibility(View.VISIBLE);
            userName.setVisibility(View.VISIBLE);
//            lv.setVisibility(View.VISIBLE);
//            level.setVisibility(View.VISIBLE);
            minePresenter.getInf(s);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent;

        switch (v.getId()) {
            case R.id.head2:
                startActivity(new Intent(getActivity(),PersonalDataActivity.class));
                back = false;
                break;
            //收货地址
            case R.id.myAddress:
                if (areYouLogin()){
                    return;
                }
                startActivity(new Intent(getActivity(), MyAddressActivity.class));
                break;
            //我的消息
            case R.id.mineMessage:
                if (areYouLogin()){
                    return;
                }
                intent = new Intent(getActivity(), MessageActivity.class);
                startActivity(intent);
                break;
            //企业认证
            case R.id.qiyerenzheng:
                if (areYouLogin()){
                    return;
                }
                intent = new Intent(getActivity(), EnterpriseCertificationActivity.class);
                startActivity(intent);
                break;
            //我的余额
            case R.id.myMoney:
                if (areYouLogin()){
                    return;
                }
                intent = new Intent(getActivity(), BalanceActivity.class);
                startActivity(intent);
                break;
            //头像
            case R.id.userHead:
                startActivity(new Intent(getActivity(), LoginActivity.class));
                back = false;
                break;
            //登录注册
            case R.id.loginRegistration:
                startActivity(new Intent(getActivity(), LoginActivity.class));
                back = false;
                break;
            case R.id.waitingList:
                if (areYouLogin()){
                    return;
                }
                intent = new Intent(getActivity(), MyOrderActivity.class);
                intent.putExtra("position", 0);
                startActivity(intent);
                break;
            case R.id.waitingSend:
                if (areYouLogin()){
                    return;
                }
                intent = new Intent(getActivity(), MyOrderActivity.class);
                intent.putExtra("position", 1);
                startActivity(intent);
                break;
            case R.id.waitingSign:
                if (areYouLogin()){
                    return;
                }
                intent = new Intent(getActivity(), MyOrderActivity.class);
                intent.putExtra("position", 2);
                startActivity(intent);
                break;
            case R.id.overOrder:
                if (areYouLogin()){
                    return;
                }
                intent = new Intent(getActivity(), MyOrderActivity.class);
                intent.putExtra("position", 3);
                startActivity(intent);
                break;
            case R.id.allOrder:
                if (areYouLogin()){
                    return;
                }
                intent = new Intent(getActivity(), MyOrderActivity.class);
                intent.putExtra("position", 4);
                startActivity(intent);
                break;
            //设置
            case R.id.setting:
                intent = new Intent();
                intent.setClass(getActivity(), SetupActivity.class);
                startActivity(intent);
                back = false;
                break;
            case R.id.scan:
                if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    // Do not have the permission of camera, request it.
                    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, REQ_CODE_PERMISSION);
                } else {
                    // Have gotten the permission
                    startCaptureActivityForResult();
                }

        }
    }

    private void startCaptureActivityForResult() {
        Intent intent = new Intent(getActivity(), CaptureActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean(CaptureActivity.KEY_NEED_BEEP, CaptureActivity.VALUE_BEEP);
        bundle.putBoolean(CaptureActivity.KEY_NEED_VIBRATION, CaptureActivity.VALUE_VIBRATION);
        bundle.putBoolean(CaptureActivity.KEY_NEED_EXPOSURE, CaptureActivity.VALUE_NO_EXPOSURE);
        bundle.putByte(CaptureActivity.KEY_FLASHLIGHT_MODE, CaptureActivity.VALUE_FLASHLIGHT_OFF);
        bundle.putByte(CaptureActivity.KEY_ORIENTATION_MODE, CaptureActivity.VALUE_ORIENTATION_AUTO);
        bundle.putBoolean(CaptureActivity.KEY_SCAN_AREA_FULL_SCREEN, CaptureActivity.VALUE_SCAN_AREA_FULL_SCREEN);
        bundle.putBoolean(CaptureActivity.KEY_NEED_SCAN_HINT_TEXT, CaptureActivity.VALUE_SCAN_HINT_TEXT);
        intent.putExtra(CaptureActivity.EXTRA_SETTING_BUNDLE, bundle);
        startActivityForResult(intent, CaptureActivity.REQ_CODE);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQ_CODE_PERMISSION: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // User agree the permission
                    startCaptureActivityForResult();
                } else {
                    // User disagree the permission
//                    Toast.makeText(this, "You must agree the camera permission request before you use the code scan function", Toast.LENGTH_LONG).show();
                }
            }
            break;
        }
    }

    public void inmV() {
        loginRegistration.setOnClickListener(this);
        qiyerenzheng.setOnClickListener(this);
        waitingList.setOnClickListener(this);
        overOrder.setOnClickListener(this);
        allOrder.setOnClickListener(this);
        setting.setOnClickListener(this);
        scan.setOnClickListener(this);
        head2.setOnClickListener(this);
        myMoney.setOnClickListener(this);
        userHead.setOnClickListener(this);
        myAddress.setOnClickListener(this);
        waitingSend.setOnClickListener(this);
        waitingSign.setOnClickListener(this);
        mineMessage.setOnClickListener(this);
    }

    //获取个人信息
    private void getInf() {

    }

    @Override
    public void onStart() {
        super.onStart();
        if (!back){
            isLogin();
            back = true;
        }
    }

    private boolean areYouLogin(){
        if (SPUtil.getShareString(Constant.USER_ID) .equals("")){
            showToastMsg("请先登录");
            return true;
        }else {
            return false;
        }
    }
}